﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ProtoImaginer;

namespace ProtoImaginer.ProtoProject
{
    public partial class ViewProject : PhoneApplicationPage
    {
        private PageListViewModel pageItem;
        public ViewProject()
        {
            InitializeComponent();
        }
        string projectIdToSend = "";
        int selectedIndex = -1;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            DataContext = App.ViewModel;
            String parameterString = NavigationContext.QueryString["selectedItem"];
            selectedIndex = int.Parse(parameterString);
            NoticeTextBlock.Text = "There are no pages for this project: " + MainPage.MyProjects[selectedIndex].ProjectName;
            MainPage.MyProjects = MainPage.FetchAllRecords();
            projectIdToSend = MainPage.MyProjects[selectedIndex].ProjectId;
            pageTitle.Text = MainPage.MyProjects[selectedIndex].ProjectName;
            App.ViewModel.LoadPages(projectIdToSend);
            if (App.ViewModel.SearchPagesNumber() > 0)
            {
                NoticeTextBlock.Visibility = Visibility.Collapsed;
            }
        }

        private void EditProjectClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/ProtoProject/EditProject.xaml?projectIdToSend=" + projectIdToSend, UriKind.Relative));
        }

        private void AddPageClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/ProtoImage/AddPhoto.xaml?projectIdToSend=" + projectIdToSend, UriKind.Relative));
        }

        private void PlayButtonClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/ProtoDemo/ProjectDemo.xaml?pageId=" +Models.ProtoDataContext.LoadPagesByProjectId(projectIdToSend).FirstOrDefault().PageId, UriKind.Relative));
        }

        private void PageListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (PageListBox.SelectedIndex == -1)
                return;
            pageItem = (sender as ListBox).SelectedItem as PageListViewModel;
            NavigationService.Navigate(new Uri("/ProtoPage/pageView.xaml?pageId=" + pageItem.PageId, UriKind.Relative));
            PageListBox.SelectedIndex = -1;
        }

        private void Remove_MenuItem_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem selectedListBoxItem = this.PageListBox.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
            PageListViewModel itemToDelete = selectedListBoxItem.DataContext as PageListViewModel;
            Models.Page p = Models.ProtoDataContext.LoadPageByPK(itemToDelete.PageId);
            Models.ProtoDataContext.DeletePage(p);
            //DeleteSelected(MyProjects.ToList(), itemToDelete.ItemID);
            //Refresh();
            App.ViewModel.LoadPages(projectIdToSend);
        }
    }
}